
import { _decorator, Component, Node, Animation, UITransform, Size } from 'cc';
import { SkillOrigin } from './SkillOrigin';
import { ConfigKey, secondGun_specialSkill_config } from '../../manager/AppControll';
import { CEventName, DamageStruct } from '../FightControl';
import { MonsterManager } from '../monster/MonsterManager';
import { MonsterOrigin } from '../monster/MonsterOrigin';
import { SoundManager } from '../../manager/SoundManger';
import { DataHelper } from '../../system/DataHelper';
import { Tools } from '../../tools/Tools';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Skill
 * DateTime = Fri May 19 2023 14:49:27 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = Skill.ts
 * FileBasenameNoExtension = Skill
 * URL = db://assets/script/fight/skill/Skill.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('SkillMissile')
export class SkillMissile extends SkillOrigin {
    InitSkill(config: secondGun_specialSkill_config, dmg: DamageStruct): void {
        super.InitSkill(config, dmg);
        this.InitAnimation();
        this.node.on(CEventName.MoveFinish, this.PlayAnimaition, this)
    }

    PlayAnimaition() {
        if (!this.mSikllConfig) {
            this.node.destroy();
            return;
        }
        if (this.mAnSpf.length > 0) {
            this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
                this.node.getComponent(Animation).stop();
                this.node.getComponent(Animation).destroy();
                this.node.destroy();
            });
            this.node.getComponent(UITransform).contentSize = new Size(this.mSikllConfig.skillNum[0] * 100, this.mSikllConfig.skillNum[0] * 100);
            if (this.mSikllConfig.aniAudio != "") {
                SoundManager.Instance<SoundManager>().PlayEffect("gun/" + this.mSikllConfig.aniAudio, Number.parseFloat(DataHelper.GetConfigByKey(ConfigKey.gunVolume).value));
            }
            this.node.getComponent(Animation).play(this.mSikllConfig.skillAni)
        } else {
            this.node.destroy();
        }
        this.GetBeingHit();
    }

    protected onDestroy(): void {
        this.node.off(CEventName.MoveFinish, this.PlayAnimaition, this);
    }

    GetBeingHit() {
        Tools.GetLtMonster().forEach((elemt) => {
            if (elemt.position.clone().subtract(this.node.position).length() <= this.mSikllConfig.skillNum[0] * 100) {
                elemt.getComponent(MonsterOrigin).Attacked(this.mDmg);
            }
        })
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
